import request from  "../../../../utils/request";
Page({
  data: {
    rawPassword: '',
    newPassword: '',
    confirmPassword: '',
    passwordStrength: 0,
    passwordStrengthText: '密码强度',
    isFormValid: false,
    user: { userId: 0, password: '', rawPassword: '' },
  },

  // 返回上一页
  goBack() {
    wx.navigateBack()
  },

  // 输入处理
  handleRawPasswordInput(e) {
    this.setData({ 
      rawPassword: e.detail.value,
      'user.rawPassword': e.detail.value 
    })
    this.checkFormValidity()
  },

  handleNewPasswordInput(e) {
    const newPassword = e.detail.value
    this.setData({ 
      newPassword,
      'user.password': newPassword 
    })
    this.checkPasswordStrength(newPassword)
    this.checkFormValidity()
  },

  handleConfirmPasswordInput(e) {
    this.setData({ confirmPassword: e.detail.value })
    this.checkFormValidity()
  },

  // 清除输入
  clearRawPassword() {
    this.setData({ 
      rawPassword: '',
      'user.rawPassword': '' 
    })
    this.checkFormValidity()
  },

  clearNewPassword() {
    this.setData({ 
      newPassword: '',
      'user.password': '' 
    })
    this.checkPasswordStrength('')
    this.checkFormValidity()
  },

  clearConfirmPassword() {
    this.setData({ confirmPassword: '' })
    this.checkFormValidity()
  },

  // 检查密码强度
  checkPasswordStrength(password) {
    let strength = 0
    let strengthText = '弱'
    
    if (password.length >= 6) strength++
    if (/[A-Z]/.test(password)) strength++ 
    if (/[0-9]/.test(password)) strength++
    if (/[^A-Za-z0-9]/.test(password)) strength++
    
    // 限制最大强度为3
    strength = Math.min(strength, 3)
    if (strength === 0) {
      strengthText = '密码强度'
    } else if (strength === 1) {
      strengthText = '弱'
    } else if (strength === 2) {
      strengthText = '中'
    } else {
      strengthText = '强'
    }
    
    this.setData({
      passwordStrength: strength,
      passwordStrengthText: strengthText
    })
  },

  // 检查表单有效性
  checkFormValidity() {
    const { rawPassword, newPassword, confirmPassword } = this.data
    const isValid = 
                   rawPassword && 
                   newPassword.length >= 6 && 
                   newPassword === confirmPassword
    this.setData({ isFormValid: isValid })
  },

  // 提交表单
  handleSubmit() {
    if (!this.data.isFormValid) return
    
    wx.showLoading({
      title: '修改中...',
      mask: true
    })
    
    let that = this;
    that.setData({
      "user.rawPassword":that.data.rawPassword
    })
    console.log("user", that.data.user);
    request({
      url: "system/user/resetPwd",
      method: "PUT",
      data: that.data.user ,
      success: (res) => {
        console.log(res);
        wx.showToast({
          title: '密码修改成功',
          icon: 'success',
          duration: 2000
        })
        // 修改成功后返回上一页
        setTimeout(() => {
          wx.navigateBack()
        }, 2000)
      },
      fail: (err) => {
        wx.showToast({
          title: '修改失败',
          icon: 'error',
          duration: 2000
        })
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },
  onLoad(options) {
    //获取用户信息
    let id = wx.getStorageSync('userid');
    let sysUserId = 0;
    let tha = this;
    request({
      url: "resident/resident/" + id,
      method: "GET",
      success: (res) => {
        sysUserId = res.data.data.sysUserId;
        tha.setData({
          'user.userId': sysUserId
        })
      }
    })
  },
})